#ifdef KITSCHY_DEBUG_MEMORY 
#include "debug_memorymanager.h"
#endif

#ifdef _WIN32
#include "windows.h"
#endif

#include <GL/gl.h>
#include <GL/glut.h>

#include "stdio.h"
#include "math.h"
#include "BVector.h"
#include "CMC.h"
#include "3dobject.h"

#include "bitmap.h"

#include "myglutaux.h"



int nearest2pow(int n)
{
	int v[]={2,4,8,16,32,64,128,256,512,-1};
	int better,dif,i,tmp;

	better=2;
	dif=n-v[0];
        if (dif<0) dif=-dif;

	i=0;
	while(v[i]!=-1) {
                tmp=n-v[i];
                if (tmp<0) tmp=-tmp;
		if (tmp<dif) {
			better=v[i];
			dif=tmp;
		} /* if */ 
		i++;
	} /* while */ 

	return better;
} /* nearest2pow */ 


void glutSolidBox(float dx,float dy,float dz)
{
	glBegin(GL_TRIANGLES);

	/* UP */ 
	glNormal3f(0,-1.0,0);
	glVertex3f(-dx,-dy,-dz);
	glVertex3f( dx,-dy,-dz);
	glVertex3f( dx,-dy, dz);

	glVertex3f( dx,-dy, dz);
	glVertex3f(-dx,-dy, dz);
	glVertex3f(-dx,-dy,-dz);

	/* DOWN */ 
	glNormal3f(0,1.0,0);
	glVertex3f( dx, dy, dz);
	glVertex3f( dx, dy,-dz);
	glVertex3f(-dx, dy,-dz);

	glVertex3f(-dx, dy,-dz);
	glVertex3f(-dx, dy, dz);
	glVertex3f( dx, dy, dz);

	/* LEFT */ 
	glNormal3f(-1.0,0,0);
	glVertex3f(-dx,-dy,-dz);
	glVertex3f(-dx, dy,-dz);
	glVertex3f(-dx, dy, dz);

	glVertex3f(-dx, dy, dz);
	glVertex3f(-dx,-dy, dz);
	glVertex3f(-dx,-dy,-dz);

	/* RIGHT */ 
	glNormal3f(1.0,0,0);
	glVertex3f( dx, dy, dz);
	glVertex3f( dx, dy,-dz);
	glVertex3f( dx,-dy,-dz);

	glVertex3f( dx,-dy,-dz);
	glVertex3f( dx,-dy, dz);
	glVertex3f( dx, dy, dz);

	/* FRONT */ 
	glNormal3f(0.0,0,1.0);
	glVertex3f(-dx,-dy, dz);
	glVertex3f( dx,-dy, dz);
	glVertex3f( dx, dy, dz);

	glVertex3f( dx, dy, dz);
	glVertex3f(-dx, dy, dz);
	glVertex3f(-dx,-dy, dz);

	/* BACK */ 
	glNormal3f(0.0,0,-1.0);
	glVertex3f( dx, dy,-dz);
	glVertex3f( dx,-dy,-dz);
	glVertex3f(-dx,-dy,-dz);

	glVertex3f(-dx,-dy,-dz);
	glVertex3f(-dx, dy,-dz);
	glVertex3f( dx, dy,-dz);

	glEnd();
} /* glutSolidBox */ 


void drawEllipse(float xradius, float yradius)
{
   glBegin(GL_LINE_LOOP);
 
   for (int i=0; i < 360; i+=2)
   {
      //convert degrees into radians
      float degInRad = i*(3.141592653589F)/180.0F;
      glVertex2f(float(cos(degInRad)*xradius),float(sin(degInRad)*yradius));
   }
 
   glEnd();
} /* drawEllipse */ 


void drawFilledEllipse(float xradius, float yradius)
{
   glBegin(GL_TRIANGLE_FAN);
 
   glVertex2f(0,0);
   for (int i=0; i <= 360; i+=2)
   {
      //convert degrees into radians
      float degInRad = i*(3.141592653589F)/180.0F;
      glVertex2f(float(cos(degInRad)*xradius),float(sin(degInRad)*yradius));
   }
 
   glEnd();
} /* drawFilledEllipse */ 



unsigned int createTexture(char *fname)
{
	unsigned int tname=0;
	int i,j,k,val;
	int sz;
	GLubyte *textura;
	Bitmap *bmp=NULL;

	bmp=new Bitmap(fname);
 
	if (bmp->valid()) {
		sz=bmp->getdx();
		if (bmp->getdy()<sz) sz=bmp->getdy();
		textura=new GLubyte[sz*sz*4];
		for(j=0,k=0;j<sz;j++) {
			for(i=0;i<sz;i++) {
				val=i+j*bmp->getdx();
				textura[k++]=bmp->getr(val);
				textura[k++]=bmp->getg(val);
				textura[k++]=bmp->getb(val);
				textura[k++]=255;
			} /* for */ 
		} /* for */ 

		glGenTextures(1,&tname);
		glPixelStorei(GL_UNPACK_ALIGNMENT,tname);
		glBindTexture(GL_TEXTURE_2D,tname);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
//		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,TEXTURE_APPROXIMATION);
//		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,TEXTURE_APPROXIMATION);
		glTexImage2D(GL_TEXTURE_2D,0,4,sz,sz,0,GL_RGBA,GL_UNSIGNED_BYTE,textura);
		delete textura;
		delete bmp;
	} else {
		delete bmp;
		return 0;
	} /* if */ 

	return tname;
} /* createTexture */ 


unsigned int createTexture(char *fname,int x,int y,int sz)
{
	unsigned int tname=0;
	int i,j,k,val;
	GLubyte *textura;
	Bitmap *bmp=NULL;

	bmp=new Bitmap(fname);
 
	if (bmp->valid()) {
		textura=new GLubyte[sz*sz*4];
		for(j=0,k=0;j<sz;j++) {
			for(i=0;i<sz;i++) {
				val=(x+i)+(y+j)*bmp->getdx();
				textura[k++]=bmp->getr(val);
				textura[k++]=bmp->getg(val);
				textura[k++]=bmp->getb(val);
				textura[k++]=255;
			} /* for */ 
		} /* for */ 

		glGenTextures(1,&tname);
		glPixelStorei(GL_UNPACK_ALIGNMENT,tname);
		glBindTexture(GL_TEXTURE_2D,tname);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
//		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,TEXTURE_APPROXIMATION);
//		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,TEXTURE_APPROXIMATION);
		glTexImage2D(GL_TEXTURE_2D,0,4,sz,sz,0,GL_RGBA,GL_UNSIGNED_BYTE,textura);
		delete textura;
		delete bmp;
	} else {
		delete bmp;
		return 0;
	} /* if */ 

	return tname;
} /* createTexture */ 



